<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="800px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="wrapper">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="申请期限至" prop="extensionTime">
          <el-date-picker :disabled="operateType == 'view' || operateType == 'reply'" v-model="form.extensionTime" type="datetime"
            format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm" placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原整改时限" prop="formerTime">
          <span>{{ form.formerTime }}</span>
        </el-form-item>
        <el-form-item label="延期理由" prop="reason">
          <el-input :disabled="operateType == 'view' || operateType == 'reply'" type="textarea" :rows="4" v-model="form.reason"></el-input>
        </el-form-item>
        <el-form-item label="延期结果" prop="status" v-if="operateType != 'delay'">
          <el-radio-group v-model="form.status" :disabled="operateType != 'reply'" >
            <!-- <el-radio :label="'0'">待审批</el-radio> -->
            <el-radio :label="'1'">不批准</el-radio>
            <el-radio :label="'2'">批准</el-radio>
          </el-radio-group>
          <!-- <el-input :disabled="operateType == 'view'" v-model="form.status"></el-input> -->
        </el-form-item>
        <el-form-item label="审批意见" prop="status" v-if="operateType != 'delay'">
          <el-input v-model="form.replyOpinion" type="textarea" :rows="2" :disabled="operateType != 'reply'"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submitPostpone" v-if="operateType == 'reply'">提交</el-button>
      <el-button type="primary" size="mini" @click="submit" v-if="operateType != 'view'&&operateType != 'reply'">确定</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { doDelay, getDelayDetail,doPostpone } from "../api";
export default {
  name: "DelayDialog",
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "任务延期申请"
    },
    data: {
      type: Object,
      default: () => { }
    },
    operateType: {
      type: String,
      default: "delay"
    },
  },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      removeUserNameOptions: [],
      form: {
        recordId: "",
        extensionTime: "",
        formerTime: "",
        reason: "",
        status:"",
        replyOpinion:null,
      },
      rules:{
        extensionTime:[
          { required: true, message: "请选择时间", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      this.form.recordId = this.data.id;
      this.form.formerTime = this.data.reviseTheDates + " 00:00:00"
      if (this.operateType == "view") {
        const res = await getDelayDetail(this.data.id)
        this.form.extensionTime = res.data.extensionTime
        this.form.reason = res.data.reason
        this.form.formerTime = res.data.formerTime
        this.form.status = res.data.status
        this.form.replyOpinion = res.data.replyOpinion
      }else if(this.operateType == 'reply'){
        const res = await getDelayDetail(this.data.extensionId)
        this.form.extensionTime = res.data.extensionTime
        this.form.reason = res.data.reason
        this.form.formerTime = res.data.formerTime
        this.form.status = res.data.status
      }
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      if (this.form.extensionTime && this.form.formerTime) {
        if (new Date(this.form.formerTime).getTime() > new Date(this.form.extensionTime).getTime()) {
          this.$message.warning("延期时间不能小于原整改时间")
          return;
        }
      }
      const res1 = await this.$refs.form.validate()
      if (!res1) {
        return;
      }
      const res = await doDelay({ ...this.form, projectId: this.projectId })
      if (res.code == 200) {
        this.$emit("refresh")
        this.beforeClose()
      }
    },
   async submitPostpone(){
    this.form.id = this.data.extensionId
     const res = await doPostpone({ ...this.form, projectId: this.projectId })
     if (res.code == 200) {
      this.$message({
          message: '提交审批结果成功',
          type: 'success'
        });
        this.$emit("refresh")
        this.beforeClose()
     }
    }
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
